const app = getApp()

Page({
    data: {
      bgmList: [],
      serverUrl: "",
      videoParams:{},
    },

    onLoad: function (params) {
      var me = this
      me.setData({
        videoParams: params
      })
      wx.showLoading({
        title: '少女祈祷中',
      });
      var serverUrl = app.serverUrl
      var user = app.getGlobalUserInfo();
      wx.request({
        url: serverUrl + '/bgm/list.do' ,
        method: 'POST',
        header:{
            'content-type': 'application/json',
            'headerUserId': user.id,
            'headerUserToken': user.userToken
        },
        success: function(res){
          console.log(res.data)
          wx.hideLoading()
          if(res.data.status == 200){
            var bgmList = res.data.data
            me.setData({
              bgmList: bgmList,
              serverUrl: serverUrl
            })
          }
          else if (res.data.status == 502) {
            wx.showToast({
              title: res.data.msg,
              duration: 2000,
              icon: "none",
              success: function () {
                wx.redirectTo({
                  url: '../userLogin/login',
                })
              }
            });
          }
        }
      })
    },

    upload: function(e){
      var me = this;
      var bgmId = e.detail.value.bgmId;
      var desc = e.detail.value.desc;
      
      console.log("bgmId:" + bgmId);
      console.log("desc:" + desc);
      
      var duration = me.data.videoParams.duration;
      var tmpHeight = me.data.videoParams.tmpHeight;
      var tmpWidth = me.data.videoParams.tmpWidth;
      var tmpVideoUrl = me.data.videoParams.tmpVideoUrl;
      var tmpCoverUrl = me.data.videoParams.tmpCoverUrl;
      
      var serverUrl = app.serverUrl;
      var userInfo = app.getGlobalUserInfo();
      //上传短视频
      wx.showLoading({
        title: '上传中',
      })
      wx.uploadFile({
        url: serverUrl + '/video/upload.do',
        formData:{
          userId: userInfo.id,
          bgmId: bgmId,
          desc: desc,
          videoSeconds: duration,
          videoWidth: tmpWidth,
          videoHeight: tmpHeight
        },
        filePath: tmpVideoUrl,
        name: 'file',
        header:{
          "content-type" : "application/json",
          'headerUserId' : userInfo.id,
          'headerUserToken' : userInfo.userToken
        },
        success:function(res){
          wx.hideLoading()
          var resultData = JSON.parse(res.data)   //格式化。官方规定返回图片地址为一个json数据。
          console.log(resultData)
          if(resultData.status == 200){ 

            //上传视频封面
            var videoId = resultData.data;
            wx.uploadFile({
              name: 'file',
              url: serverUrl + '/video/uploadCover.do',
              filePath: tmpCoverUrl,
              formData:{
                userId: userInfo.id ,
                videoId: videoId
              },
              header:{
                "content-type" : "application/json",
                'headerUserId' : userInfo.id,
                'headerUserToken' : userInfo.userToken
              },
              success:function(res){
                var resultCoverData = JSON.parse(res.data)
                if(resultCoverData.status == 200){
                  wx.showToast({
                    title: '视频上传成功!',
                    icon: 'success',
                    duration: 3000
                  })
                  wx.navigateBack({
                    delta: 1,
                  })
                }
                else if (res.data.status == 502){
                  wx.showToast({
                    title: res.data.msg,
                    duration: 2000,
                    icon: "none"
                  });
                  wx.redirectTo({
                    url: '../userLogin/login',
                  })
                }
                else{
                  wx.showToast({
                    title: '视频上传失败!',
                    icon: 'none',
                    duration: 3000
                  })
                }
              },           
          })
              
          }

        }

      })
    }
  
})

